<?php

class StoreController extends \BaseController {

	/**
	 * Display a listing of the resource.
	 * API to view all the stores
	 * API to search the stores
	 * A client will provide us with search options such as city, country, and zip code,
	 * Here the API call will be like /stores?city=london
	 * 
	 * @return Response
	 */
	public function index()
	{	
		$city = Input::get('city');
		$country = Input::get('country');
		$zip = Input::get('zip');
		
		// Here first we are checking whether we have any inputs from the user
		if ($city == '' && $country == '' && $zip == '') {
			$stores = Store::all();
		} else {
			$stores = DB::table('store')
					->where('city', 'LIKE', "%$city%")
					->or_where('country', 'LIKE', "%$country%")
					->or_where('zip', '=', "$zip")
					->get();
		}
		
		return Response::json(array(
				'error' => false,
				'stores' => $stores->toArray()
			),
			200	// status code of the request
		)->setCallback(Input::get('callback'));
	}


	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		//
	}


	/**
	 * Store a newly created resource in storage.
	 * API to add a store
	 *
	 * @return Response
	 */
	public function store()
	{
		$store = new store;
		$store->name = Request::get('name');
		$store->address = Request::get('address');
		$store->city = Request::get('city');
		$store->zip = Request::get('zip');
		$store->country = Request::get('country');
		$store->latitude = Request::get('latitude');
		$store->longitude = Request::get('longitude');
		$store->support_phone = Request::get('support_phone');
		$store->support_email = Request::get('support_email');
		$store->user_id = Auth::user()->id;	// who created the store
		
		if ($store->save()) {
			return Response::json(array(
					'error' => false,
					'msg' => 'store created successfully.'
				),
				200
			)->setCallback(Input::get('callback'));
		} else {
			return Response::json( array(
					'error' => true,
					'msg' => 'issue creating store!'
				),
				200
			)->setCallback(Input::get('callback'));
		}
	}


	/**
	 * Display the specified resource.
	 * API to view an individual store
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		$store = Stores::find($id);
		
		return Response::json(array(
				'error' => false,
				'store' => $store
			),
			200
		)->setCallback(Input::get('callback'));
	}


	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
	}


	/**
	 * Update the specified resource in storage.
	 * API to update a store
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		$store = Store::find($id);
		$store->name = Request::get('name');
		$store->address = Request::get('address');
		$store->city = Request::get('city');
		$store->zip = Request::get('zip');
		$store->country = Request::get('country');
		$store->latitude = Request::get('latitude');
		$store->longitude = Request::get('longitude');
		$store->support_phone = Request::get('support_phone');
		$store->support_email = Request::get('support_email');
		$store->user_id = Auth::user()->id; // who created the store
		
		if ($store->save()) {
			return Response::json(array(
					'error' => false,
					'msg' => 'store has been updated'
				),
				200
			)->setCallback(Input::get('callback'));
		} else {
			return Response::json( array(
					'error' => true,
					'msg' => 'issue updating store!'
				),
				200
			)->setCallback(Input::get('callback'));
		}
	}


	/**
	 * Remove the specified resource from storage.
	 * API to delete a store
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		$store = Store::find($id);
		
		if ( ! empty($store)) {
			$store->delete();
		}
		
		return Response::json(array(
			'error' => false,
			'msg' => 'Store deleted'
			),
			200
		)->setCallback(Input::get('callback'));
	}


}
